﻿using Edu.Http;
using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.Product;
using Infrastructure.ExternalService.Core.Responses;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Yyd.Ordering.EduSubscriber.UserCenter.Events;

namespace Infrastructure.ExternalService.Core.Imp.EduStudent
{
    /// <summary>
    /// 升学，学员服务
    /// </summary>
    public class EduStudentExternal : BaseHttpClient, IEduStudentExternal
    {
        private readonly OrderCenterApiOptions _orderingOptions;
        public IAccessTokenProvider TokenProvider { get; set; }

        public EduStudentExternal(IOptions<OrderCenterApiOptions> options, HttpClient client, ILoggerFactory loggerFactory)
            : base(client, loggerFactory, options.Value.StudentOptions.EndPoint)
        {
            _orderingOptions = options.Value ?? throw new HttpProxyException($"{nameof(EduStudentExternal)}配置获取失败");
            loggerFactory.CreateLogger<EduStudentExternal>();
        }

        /// <summary>
        /// 设置BaseHttpClient的BaseAddress：该值默认是从构造函数传入的，但重写该方法可以改变该值
        /// </summary>
        /// <returns></returns>
        protected override string SetHttpClientBaseAddress()
        {
            return null;
        }

        public async Task<int> UpdateMobile(YouluUCUserUpdateMobile request)
        {

            //Logger.LogError($"---修改手机号，学员服务请求接口{JsonConvert.SerializeObject(request)}");
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostAsync<WrapResponse<int>>
                         (_orderingOptions.StudentOptions.UpdateMobile, request);

            //Logger.LogError($"---修改手机号，学员服务返回结果{JsonConvert.SerializeObject(response)}");
            if (response.Code.Equals(ClientReturnCode.Success))
            {
                return response.Data;
            }

            //if (response.Data == null)
            //{
            //    throw new HttpProxyException($"查询【{formStr}】电子凭证配置的外部接口返回错误:【{response.Msg}】");
            //}


            throw new HttpProxyException($"{response.Msg}");
        }
    }
}